<%@ page language="java" import="java.net.*" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登录中</title>
</head>


<body>
	<%
		request.setCharacterEncoding("utf-8");
		//判断用户是否勾选了保存用户信息的选项
		String[] isUseCookies =request.getParameterValues("isUseCookie");
		if(isUseCookies !=null && isUseCookies.length > 0)
		{
			//把用户名和密码保存在Cookie对象里，使用URLEncoder解决无法再Cookie中保存中文的问题
			String username = URLEncoder.encode(request.getParameter("username"),"utf-8");
			String password = URLEncoder.encode(request.getParameter("password"),"utf-8");
			
			Cookie usernameCookie = new Cookie("username",username);
			Cookie passwordCookie = new Cookie("password",password);
			
			//设置Cookie的最大生存期限为10天
			usernameCookie.setMaxAge(864000);
			passwordCookie.setMaxAge(864000);
			
			//保存Cookie对象
			response.addCookie(usernameCookie);
			response.addCookie(passwordCookie);
		}
		
		else
		{
			Cookie[] cookies = request.getCookies();
			if(cookies != null && cookies.length > 0)
			{
				for (Cookie c:cookies)
				{
					if (c.getName().equals("username") || c.getName().equals("password"))
					{
						c.setMaxAge(0);//设置Cookie失效
						response.addCookie(c);//重新保存Cookie对象
					}
				}
			}
		}
	
	%>

	<h1>登录成功</h1>
	<hr>
	<br>
	<br>
	<br>
		<a href="Users.jsp">查询用户信息</a>
</body>
</html>